﻿
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RegistrationDate
{
    /// <summary>
    /// 
    /// </summary>
    public class CRegEditCtl
    {
        /// <summary>
        /// 键值,初始化时，传入什么参数就用什么能数
        /// </summary>
        public string keyName = "RegisteredPath";
        /// <summary>
        /// 当为1时，软件需要注册
        /// </summary>
        public string regKeyName = "RegFlag";

        public string effectiveDateName = "effectiveDate";

        //public string effectiveDate = "2023-07-15 23:59:59";

        public CRegEditCtl() {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="timeStr">以毫秒作为随机数</param>
        /// <param name="regFlag"></param>
        /// <param name="effectiveDate"></param>
        public CRegEditCtl(string timeStr,string regFlag,string effectiveDate) {
            string dbpath = ReadRegPath(keyName);
            if (dbpath != null && dbpath != "") {
                return;
            }
            CreateKey(keyName);
            WriteRegeditEn(keyName, keyName, timeStr);//写入第一次运行的时间
            WriteRegeditEn(keyName, regKeyName, regFlag);//第一次运行是不需要注册的
            WriteRegeditEn(keyName, effectiveDateName, effectiveDate);//这是第一个有效期
        }

        public string ReadRegPath(string keyName) {
            string registData = "";
            RegistryKey hkml = Registry.CurrentUser;
            RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\" + keyName, true);
            try {
                registData = software.GetValue(keyName).ToString();
            }
            catch { }

            return registData;
        }

        public string ReadRegFlag(string regKeyName) {
            string registData = "";
            RegistryKey hkml = Registry.CurrentUser;
            RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\" + keyName, true);
            try {
                registData = software.GetValue(regKeyName).ToString();
            }
            catch { }

            return registData;
        }

        public void CreateKey(string keyName) {
            RegistryKey key = Registry.CurrentUser;
            RegistryKey software = key.CreateSubKey(@"SOFTWARE\" + keyName);
        }

        public void WriteRegeditEn(string keyName, string name, string tovalue) {
            RegistryKey hklm = Registry.CurrentUser;
            RegistryKey software = hklm.OpenSubKey(@"SOFTWARE\" + keyName, true);
            string enVal = AESModel.AESEncrypt(tovalue);//写入时需要加密
            software.SetValue(name, enVal);
        }

        public void WriteRegedit(string keyName, string name, string tovalue)
        {
            RegistryKey hklm = Registry.CurrentUser;
            RegistryKey software = hklm.OpenSubKey(@"SOFTWARE\" + keyName, true);
            //string enVal = AESModel.AESEncrypt(tovalue);//写入时需要加密
            software.SetValue(name, tovalue);
        }

        /// <summary>
        /// 读注册列表，带解码功能
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public string ReadRegeditDe(string keyName, string name) {
            string str = ReadRegedit(keyName, name) as string;
            if (str == null) return str;
            return AESModel.AESDecrypt(str);
        }

        public object ReadRegedit(string keyName, string name) {
            RegistryKey hklm = Registry.CurrentUser;
            RegistryKey software = hklm.OpenSubKey(@"SOFTWARE\" + keyName, true);
            if (software == null) return null;
            return software.GetValue(name);
        }

        public void DeleteKey(string keyName) {
            RegistryKey key = Registry.CurrentUser;
            try {
                key.DeleteSubKey(@"SOFTWARE\" + keyName);
            }
            catch { }
        }
    }
}
